package com.ruoyi.web.controller.basicData.categoryManagement;

import com.ruoyi.basicData.categoryManagement.service.ICategoryManagementService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.CategoryManagement;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类管理Controller
 * 
 * @author Yuan
 * @date 2024-05-03
 */
@RestController
@RequestMapping("/basicData/categoryManagement")
public class CategoryManagementController extends BaseController
{
    @Autowired
    private ICategoryManagementService categoryManagementService;

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:list')")
    @GetMapping("/list")
    public AjaxResult list(CategoryManagement categoryManagement)
    {
        List<CategoryManagement> list = categoryManagementService.selectCategoryManagementList(categoryManagement);
        return success(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryManagement categoryManagement)
    {
        List<CategoryManagement> list = categoryManagementService.selectCategoryManagementList(categoryManagement);
        ExcelUtil<CategoryManagement> util = new ExcelUtil<CategoryManagement>(CategoryManagement.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(categoryManagementService.selectCategoryManagementByCategoryId(categoryId));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryManagement categoryManagement)
    {
        return toAjax(categoryManagementService.insertCategoryManagement(categoryManagement));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryManagement categoryManagement)
    {
        return toAjax(categoryManagementService.updateCategoryManagement(categoryManagement));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:categoryManagement:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable("categoryId") Long categoryId)
    {
        if (categoryManagementService.hasChildByCategoryId(categoryId))
        {
            return warn("存在子类别,不允许删除");
        }
        if (categoryManagementService.checkCategoryUseStatus(categoryId))
        {
            return warn("分类已被绑定,不允许删除");
        }
        return toAjax(categoryManagementService.deleteCategoryManagementByCategoryId(categoryId));
    }

    /**
     * 获取分类树列表
     */
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree(CategoryManagement categoryManagement)
    {
        return success(categoryManagementService.selectCategoryTreeList(categoryManagement));
    }

}
